package com.zimmor.mq.rocket_train1.order;

import com.zimmor.mq.RocketMQProperties;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2023/2/16 16:24
 * @Description 顺序消息发送
 * @version: 1.0.0
 */

public class Producer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer producer = new DefaultMQProducer("producer-group1");
        producer.setNamesrvAddr(RocketMQProperties.NAME_SRV_ADDR);
        producer.start();
        for (int i = 0; i < 100; i++) {
            int orderId = i % 10;
            Message message = new Message("order", "tag", orderId+"", ("orderMessage" + i).getBytes());
            SendResult send = producer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    Integer id = (Integer) o;
                    return list.get((id % list.size()));
                }
            }, orderId);
            System.out.println("send = " + send);
        }
        producer.shutdown();
    }
}
